package top.dbpanda.cmsjpa.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.dbpanda.cmsjpa.common.CmsResponse;
import top.dbpanda.cmsjpa.entity.UserEntity;
import top.dbpanda.cmsjpa.model.dto.AddUserDTO;
import top.dbpanda.cmsjpa.model.dto.EditUserDTO;
import top.dbpanda.cmsjpa.repository.UserRepository;
import top.dbpanda.cmsjpa.service.UserService;

import java.util.List;
import java.util.Optional;

/**
 * @author 20018954
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户控制器")
public class UserController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserService userService;

    @PostMapping("/add")
    @ApiOperation("添加用户")
    public CmsResponse<UserEntity> add(@RequestBody @Validated AddUserDTO addUser) {
        return CmsResponse.success(userService.add(addUser));
    }

    /**
     * 5 38 13 25.6
     * 20 108 15 63.4
     * 1000 9699 20 5039.407
     * @param addUser
     * @return
     */
    @PostMapping("/testLock")
    @ApiOperation("添加用户")
    public CmsResponse<UserEntity> testLock(@RequestBody @Validated AddUserDTO addUser) {
        return CmsResponse.success(userService.testLock(addUser));
    }

    /**
     * 并发测试 5 30 11 21.2
     *        20 109 20 70.3
     *        2000 16002 20 8065.586
     * @param addUser
     * @return
     */
    @PostMapping("/testSynchronized")
    @ApiOperation("添加用户")
    public CmsResponse<UserEntity> testSynchronized(@RequestBody @Validated AddUserDTO addUser) {
        return CmsResponse.success(userService.testSynchronized(addUser));
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户")
    public void delete(Long userId) {
        Optional<UserEntity> byId = userRepository.findById(userId);
        if (byId.isPresent()) {
            userRepository.deleteById(userId);
        } else {
            throw new RuntimeException(userId + "不存在");
        }
    }

    @PutMapping("/edit")
    @ApiOperation("编辑用户")
    public UserEntity edit(@RequestBody EditUserDTO editUser) {
        UserEntity userEntity = BeanUtil.copyProperties(editUser, UserEntity.class);
        return userRepository.save(userEntity);
    }

    @GetMapping("/view")
    @ApiOperation("查看用户")
    @Cacheable()
    public UserEntity view(Long userId) {
        Optional<UserEntity> byId = userRepository.findById(userId);
        return byId.orElse(null);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询用户")
    public Page<UserEntity> page() {
        PageRequest pageRequest = PageRequest.of(0, 10);
        return userRepository.findAll(pageRequest);
    }

    @GetMapping("/list")
    @ApiOperation("分页查询用户")
    public List<UserEntity> list() {
        return userRepository.findAll();
    }
}
